<template>
  <view class="w-100 h80 fixed left0 px-2" :style="'top:' + top + 'rpx'">
    <view v-if="type" class="w80 h80 f-c-c re z-999" @click="set_back">
      <image src="../static/icon_back.png" class="w64 h64" />
    </view>
    <view v-else class="w80 h80 f-c-c re z-999" @click="set_scanCode">
      <image src="../static/icon_scan.png" class="w64 h64" />
    </view>
    <view
      class="w-100 ab top0 left0 h-100 f-c-c font35 t-20 font700 z-99"
      v-if="title"
      >{{ title }}</view
    >
  </view>
</template>

<script>
import { mapState, mapActions } from "vuex";
export default {
  props: {
    type: {},
    title: {
      default: "",
      type: String,
    },
    to: {},
  },
  computed: {
    ...mapState("user", ["isLogin"]),
  },
  data() {
    return {
      top: 0, // 状态栏高度
      info: "2222",
    };
  },
  created() {
    const { statusBarHeight } = uni.getStorageSync("systemInfo");
    this.top = statusBarHeight / (uni.upx2px(100) / 100); // 状态栏高度
    let toprpx = this.top + 80 + 20; // 100为 header 20 为间距
    uni.setStorageSync("toprpx", toprpx);
  },
  methods: {
    set_scanCode() {
      if (!this.isLogin) {
        return uni.showToast({
          title: "请先登录",
          icon: "none",
        });
      }
      let that = this;
      uni.scanCode({
        // scanType:['datamatrix'],
        success(res) {
          console.log("1:" + decodeURIComponent(res.rawData));
          console.log("条码类型：" + JSON.stringify(res));
          // console.log('条码内容：' + res);
          if (res.path) {
            uni.redirectTo({
              url: res.path,
            });
          } else {
            uni.showToast({
              title: "未扫码到任何信息",
              icon: "none",
            });
          }
          // this.info = JSON.stringify(res)
        },
        fail(srr) {
          console.log("ss", srr);
        },
      });
    },
    set_back() {
      const page = getCurrentPages();
      console.log("page", page);
      // to 代表已进入多层套娃 需要坐飞机到第一层
      if (this.to) {
        uni.redirectTo({
          url: "/" + page[0].route,
        });
      } else {
        if (page.length > 1) {
          if (page[page.length - 1].route == "pages/my/order/details") {
            // uni.reLaunch({
            //   url: "/pages/orderPage/index",
            // });
            uni.reLaunch({
              url: "/pages/index/index?active=3",
            });
          } else if (page[page.length - 1].route == "pages/my/order/order") {
            uni.reLaunch({
              url: "/pages/index/index",
            });
          } else {
            uni.navigateBack();
          }
        } else {
          uni.redirectTo({
            url: "/pages/login/index",
          });
        }
      }
    },
    getQueryString(url) {
      // 通过 ? 分割获取后面的参数字符串
      let urlStr = url.split("?")[1];
      // 创建空对象存储参数
      let obj = {};
      // 再通过 & 将每一个参数单独分割出来
      let paramsArr = urlStr.split("&");
      for (let i = 0, len = paramsArr.length; i < len; i++) {
        // 再通过 = 将每一个参数分割为 key:value 的形式
        let arr = paramsArr[i].split("=");
        obj[arr[0]] = arr[1];
      }
      return obj;
    },
  },
};
</script>

<style></style>
